package Ar;

/**
 * @Author MDD
 * @Description
 * @Version
 * @Date
 **/
public class ArrayBinarySeach {
    public static int binarySearch(int[] arr,int num){
        int low = 0;
        int high = arr.length-1;
        while (low<=high){
            int mid = (high+low)/2;
            if (arr[mid]<num){
                low = mid+1;
            }
            else if (arr[mid]>num){
                high = mid-1;
            }
            else {
                return mid;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] arr = {5,66,87,98,154,366,877,981};
        int index = binarySearch(arr,87);
        System.out.println("二分法查找测试：");
        System.out.println("key=87的下标为："+index);
    }
}
